//! 基于auxm的controller

use ane_system::dao::{SysDict, SysDictDataSimpleVo, SysDictVo};
use axum::{extract::Path, response::IntoResponse, routing::get, Extension, Router};

use crate::{
    error::{AppError, AppResult},
    extract::Json,
    state::AppState,
};
pub mod account;
pub mod auth;
#[cfg(feature = "cms")]
pub mod cms;
pub mod monitor;
pub mod sql_view;
pub mod system;

pub fn get_route<S>(prefix: &str) -> Router<S>
where
    S: Sync + Send + Clone + 'static,
{
    let f = move |p: &str| -> String { format!("{}{p}", prefix) };
    Router::new()
        .route(&f("/dict/:dict_type"), get(get_dict))
        .route(&f("/monitor/server"), get(monitor::monitor_server))
}

async fn get_dict(
    Extension(state): Extension<AppState>,
    Path(dict_type): Path<String>,
) -> AppResult<Json<Vec<SysDictDataSimpleVo>>> {
    SysDict::list_dict_data_by_type(state.db(), dict_type, Some(true))
        .await
        .map(Json)
        .map_err(Into::into)
}
